<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <!-- 3天前 日期插件-->
      <van-cell
        center
        :title="artObj.aut_name"
        :label="formmatData(artObj.pubdate)"
      >
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <!-- artObj.is_followed 看后台 是false 不喜欢-未关注/true喜欢-关注 -->
            <van-button
              type="info"
              size="mini"
              v-if="artObj.is_followed"
              @click="followedFn(false)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="followedFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <!-- 可以使用同一个方法，用不同值区分-->
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="artObj.attitude === 1"
          @click="loveFn(false)"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="loveFn(true)"
          >点赞</van-button
        >
      </div>
    </div>

    <!-- 评论列表 导入组件-->
    <comment-list :artId="$route.query.aid"></comment-list>
  </div>
</template>

<script>
import { getArticleDetailAPI, followedAutAPI, unFollowedAutAPI, articleLikeAPI, articleDisLikeAPI } from '@/api'

// 日期组件--复制--从articleItem
import * as dayjs from 'dayjs'
import * as relativeTime from 'dayjs/plugin/relativeTime' // import plugin
import 'dayjs/locale/zh-cn' // import locale 集成中文

import CommentList from './CommentList.vue'

dayjs.extend(relativeTime) // use plugin
dayjs.locale('zh-cn') // use locale
// 系统时间到之前指定时间的距离值

export default {
  components: { CommentList },
  data () {
    return {
      artObj: {}
    }
  },
  async created () {
    // 发请求： 携带跳转时的路由信息，localhost:8080/#/article_detail?aid=8213
    const res = await getArticleDetailAPI({
      artId: this.$route.query.aid // artId ?后面拼接的内容
    })
    console.log(res)
    this.artObj = res.data.data // 调用接口拿到文章详情数据, 并保存到data变量上
  },
  methods: {
    formmatData (dateStr) {
      var a = dayjs() // 当前时间
      var b = dayjs(dateStr) // 文章发布时间 获取到
      // a.to(b) // in 2 years 离现在多长时间
      // console.log(a.to(b))
      return a.to(b)
    },
    // ‘关注’和‘取关’按钮-点击事件
    async followedFn (bool) {
      // this.artObj.is_followed = bool

      if (bool === true) {
        this.artObj.is_followed = true // 用户点了（关注）按钮--显示“已关注”按钮
        const res = await followedAutAPI(
          this.artObj.aut_id // 传参：关注用户的id
        )
        console.log(res)
        console.log(this.artObj.aut_id)
      } else {
        this.artObj.is_followed = false // 用户未关注（取关）点了【已关注】--显示“关注”按钮
        const res = await unFollowedAutAPI(
          this.artObj.aut_id // 传参：关注用户的id
        )
        console.log(res)
        console.log(this.artObj.aut_id)
      }
    },
    // 文章点赞/取消点赞  用户对文章的态度, -1: 无态度，0-不喜欢，1-点赞
    async loveFn (bool) { // 形参
      console.log(bool)

      if (bool === true) { // 用户点【点赞】  显示 => 【已点赞】按钮
        this.artObj.attitude = 1 // v-if显示
        await articleLikeAPI(
          this.$route.query.aid
        )
      } else { // 用户点了【已点赞】取赞      显示 => 【点赞】按钮
        this.artObj.attitude = 0
        await articleDisLikeAPI(
          this.$route.query.aid
        )
      }
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
